package sixkey.controller;

/**
 * ClassName:UserPermissionController
 * Package:sixkey.controller
 * Description
 *
 * @Author:@wenxueshi
 * @Create:2023/6/22 - 17:12
 * @Version:v1.0
 */

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import sixkey.Exception.UserNameExistException;
import sixkey.annotation.PermissionAnnotation;
import sixkey.annotation.UserPermissionAnnotation;
import sixkey.domain.entity.User;
import sixkey.domain.vo.UserVo;
import sixkey.service.IUserService;
import sixkey.utils.ResResult;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户权限校验
 */
@Api(tags = "用户权限校验")
@RestController
@RequestMapping("/user/permission")
@RequiredArgsConstructor
public class UserPermissionController {

    private final IUserService userService;

    //增加用户
    @ApiOperation(value = "添加用户")
    @PostMapping("/add/{userId}")
    @UserPermissionAnnotation(value = "user:add")
    public ResResult add(@PathVariable("userId") Long userId, @RequestBody User user) throws UserNameExistException {
        userService.RegisterUser(user);
        return ResResult.okResult();
    }

    //修改用户
    @ApiOperation(value = "修改用户",httpMethod = "PUT",response = ResResult.class,notes = "修改用户")
    @PutMapping("/update/{userId}")
    @UserPermissionAnnotation(value = "user:update")
    public ResResult updateUser(@PathVariable("userId") Long userId,@RequestBody User user){
        userService.updateUser(user);
        return ResResult.okResult();
    }

    //获取用户
    @ApiOperation(value = "获取用户")
    @GetMapping("/getById/{userId}")
    @UserPermissionAnnotation(value = "user:get")
    public ResResult getById(@PathVariable("userId") Long userId){
        List<User> userList = userService.list();
        return ResResult.okResult(userList);
    }

    //删除用户
    @ApiOperation(value = "删除用户",httpMethod = "DELETE",response = ResResult.class,notes = "删除用户")
    @DeleteMapping("/delete/{userId}/{id}")
    @UserPermissionAnnotation(value = "user:delete")
    public ResResult updateUser(@PathVariable("userId") Long userId,@PathVariable("id") Long id){
        userService.removeById(id);
        return ResResult.okResult();
    }
}
